package org.example.myleet.p953;

public class Solution {
    public boolean isAlienSorted(String[] words, String order) {
        //录入题目给定的order
        int[] alphabetOrder = new int[26];
        for (int i = 0; i < order.length(); ++i) {
            alphabetOrder[order.charAt(i) - 'a'] = i;
        }
        for (int i = 0; i < words.length - 1; ++i) {
            String w1 = words[i];
            String w2 = words[i + 1];
            int maxLen = Math.max(w1.length(), w2.length());
            for (int j = 0; j < maxLen; ++j) {
                //如果j超出某个字符长度，按空字符处理，空字符优先度最高，比0小，给-1
                int o1 = j >= w1.length() ? -1 : alphabetOrder[w1.charAt(j) - 'a'];
                int o2 = j >= w2.length() ? -1 : alphabetOrder[w2.charAt(j) - 'a'];
                if (o1 > o2) {
                    return false;
                } else if (o1 < o2) {
                    break;
                }
            }
        }
        return true;
    }
}
